
package kaizen.jdbc;

import java.io.IOException;
import java.io.PrintStream;

import java.sql.*;

import java.util.Properties;
import java.util.Vector;

import kaizen.nomweb.Historico;
import kaizen.nomweb.utilerias;

// Referenced classes of package kaizen.jdbc:
//            dbCon, dbCursor, dbRecord

public final class cMgr
{

  public cMgr()
  {
  }

  public static void First(String s, String s1)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
      {
        dbcon.First(s1);
        return;
      }
      break;
    }

  }

  public static void Last(String s, String s1)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
      {
        dbcon.Last(s1);
        return;
      }
      break;
    }

  }

  public static void Next(String s, String s1)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
      {
        dbcon.Next(s1);
        return;
      }
      break;
    }

  }

  public void Previous(String s, String s1)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
      {
        dbcon.Previous(s1);
        return;
      }
      break;
    }

  }

  public static boolean add(String s, String s1, String s2, String s3, 
                            String s4) throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    boolean flag = false;
    ls_driver = s1;
    ls_URL = s2;
    ls_dbUsr = s3;
    ls_dbPwd = s4;
    try
    {
      Driver driver = (Driver)Class.forName(ls_driver).newInstance();
      try
      {
        Connection connection = 
          DriverManager.getConnection(ls_URL, ls_dbUsr, ls_dbPwd);
        vConnection.addElement(new dbCon(s, connection, true));
        flag = true;
      } catch (SQLException sqlexception)
      {
        String s6 = "ERROR   -2 : " + sqlexception;
        Historico historico1 = new Historico(arch_log, true);
        historico1.agregar(s6);
        historico1.cerrar();
        System.out.println(s6);
      }
    } catch (Exception exception)
    {
      String s5 = "ERROR   -1 : " + exception;
      Historico historico = new Historico(arch_log, true);
      historico.agregar(s5);
      historico.cerrar();
      System.out.println(s5);
    }
    return flag;
  }

  public static void close(String s) throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      Connection connection = dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        try
        {
          dbcon.close(s);
          connection.close();
          vConnection.removeElementAt(i);
          return;
        } catch (SQLException sqlexception)
        {
          String s1 = "ERROR    -7 :" + sqlexception;
          Historico historico = new Historico(arch_log, true);
          historico.agregar(s1);
          historico.cerrar();
          System.out.println(s1);
          return;
        }
      break;
    }

  }

  public static void closeAll() throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      Connection connection = dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        try
        {
          connection.close();
        } catch (SQLException sqlexception)
        {
          String s = "ERROR    -7 :" + sqlexception;
          Historico historico = new Historico(arch_log, true);
          historico.agregar(s);
          historico.cerrar();
          System.out.println(s);
        }
    }

    vConnection.removeAllElements();
  }

  public static void commit(String s)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (dbcon.getName().equals(s))
        dbcon.commit();
    }

  }

  public static void executeQuery(String s, String s1, 
                                  String s2) throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      Connection connection = dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        try
        {
          java.sql.CallableStatement callablestatement = 
            connection.prepareCall("alter session set NLS_NUMERIC_CHARACTERS = '.,'");
          callablestatement.execute();
          Statement statement = connection.createStatement();
          try
          {
            if (statement.execute(s2))
              try
              {
                ResultSet resultset = statement.getResultSet();
                ResultSetMetaData resultsetmetadata = resultset.getMetaData();
                dbcon.addCursor(new dbCursor(s1, resultset, 
                                             resultsetmetadata));
                return;
              } catch (SQLException sqlexception2)
              {
                String s4 = "ERROR   -5 :" + sqlexception2;
                Historico historico1 = new Historico(arch_log, true);
                historico1.agregar(s4);
                historico1.cerrar();
                System.out.println(s4);
                return;
              }
          } catch (SQLException sqlexception1)
          {
            String s5 = "ERROR   -4 : " + sqlexception1;
            Historico historico2 = new Historico(arch_log, true);
            historico2.agregar(s2);
            historico2.agregar(s5);
            historico2.cerrar();
            System.out.println(s5);
            return;
          }
        } catch (SQLException sqlexception)
        {
          String s3 = "ERROR   -3 : " + sqlexception;
          Historico historico = new Historico(arch_log, true);
          historico.agregar(s3);
          historico.cerrar();
          System.out.println(s3);
          return;
        }
      break;
    }

  }

  public static boolean executeStatement(String s, 
                                         String s1) throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    boolean flag = false;
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      Connection connection = dbcon.getConnection();
      boolean flag1 = dbcon.getStatus();
      if (flag1)
        try
        {
          Statement statement = connection.createStatement();
          try
          {
            statement.execute(s1);
            flag = true;
          } catch (SQLException sqlexception1)
          {
            String s3 = "ERROR   -4 : " + sqlexception1;
            Historico historico1 = new Historico(arch_log, true);
            historico1.agregar(s3);
            historico1.cerrar();
            System.out.println(s3);
          }
        } catch (SQLException sqlexception)
        {
          String s2 = "ERROR   -3 : " + sqlexception;
          Historico historico = new Historico(arch_log, true);
          historico.agregar(s2);
          historico.cerrar();
          System.out.println(s2);
        }
      break;
    }

    return flag;
  }

  public static String executeStatementForms(String s, 
                                             String s1) throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    String s2 = "";
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      Connection connection = dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        try
        {
          Statement statement = connection.createStatement();
          try
          {
            statement.execute(s1);
          } catch (SQLException sqlexception1)
          {
            s2 = "ERROR   -4 : " + sqlexception1;
            Historico historico1 = new Historico(arch_log, true);
            historico1.agregar(s2);
            historico1.cerrar();
          }
        } catch (SQLException sqlexception)
        {
          s2 = "ERROR   -3 : " + sqlexception;
          Historico historico = new Historico(arch_log, true);
          historico.agregar(s2);
          historico.cerrar();
        }
      break;
    }

    System.out.println(s2);
    return s2;
  }

  public static Vector getAllRecords(String s, String s1)
  {
    Vector vector = new Vector();
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        vector = dbcon.getAllRecords(s1);
      break;
    }

    return vector;
  }

  public static String[] getAllRows(String s, String s1)
  {
    String as[] = null;
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        as = dbcon.getAllRows(s1);
      break;
    }

    return as;
  }

  public static Connection getConnection(String s) throws IOException
  {
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      Connection connection = dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        return connection;
      break;
    }

    return null;
  }

  public static ResultSetMetaData getMetaData(String s, String s1)
  {
    ResultSetMetaData resultsetmetadata = null;
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        resultsetmetadata = dbcon.getResultsMD(s1);
      break;
    }

    return resultsetmetadata;
  }

  public static dbRecord getRecord(String s, String s1)
  {
    dbRecord dbrecord = null;
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        dbrecord = dbcon.getRecord(s1);
      break;
    }

    return dbrecord;
  }

  public static String getRow(String s, String s1)
  {
    String s2 = null;
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        s2 = dbcon.getRow(s1);
      break;
    }

    return s2;
  }

  public static boolean getStatus(String s)
  {
    Vector vector = new Vector();
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (dbcon.getName().equals(s))
      {
        dbcon.getConnection();
        return dbcon.getStatus();
      }
    }

    return false;
  }

  public void init() throws IOException
  {
    prop = utilerias.loadProperties("/kaizen/general.properties");
    arch_log = prop.getProperty("log_Err_Sql");
    System.out.println("Entre al Init de cMgr");
  }

  public static void removeCursor(String s, String s1)
  {
    Vector vector = new Vector();
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (!dbcon.getName().equals(s))
        continue;
      dbcon.getConnection();
      boolean flag = dbcon.getStatus();
      if (flag)
        dbcon.close(s1);
      break;
    }

  }

  public static void rollback(String s)
  {
    Object obj = null;
    for (int i = 0; i < vConnection.size(); i++)
    {
      dbCon dbcon = (dbCon)vConnection.elementAt(i);
      if (dbcon.getName().equals(s))
        dbcon.rollback();
    }

  }

  private static String ls_driver;
  private static String ls_URL;
  private static String ls_dbUsr;
  private static String ls_dbPwd;
  private static Vector vConnection = new Vector();
  private static Vector sConnection = new Vector();
  private static Properties prop = new Properties();
  private static String arch_log;

}
